package net.entelijan.cobean.examples.data.literature.test;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.namespace.QName;

import net.entelijan.cobean.examples.data.literature.endpoint.Country;
import net.entelijan.cobean.examples.data.literature.endpoint.Language;
import net.entelijan.cobean.examples.data.literature.endpoint.LitAuthor;
import net.entelijan.cobean.examples.data.literature.endpoint.LitBook;
import net.entelijan.cobean.examples.data.literature.endpoint.LitBookSearchItem;
import net.entelijan.cobean.examples.data.literature.endpoint.OrderAttribute;
import net.entelijan.cobean.examples.data.literature.endpoint.WsLitService;
import net.entelijan.cobean.examples.data.literature.endpoint.WsLitService_Service;
import net.entelijan.cobean.examples.data.literature.wrapper.WsUtil;

@SuppressWarnings("unused")
public class RunLitService {

	private static Random ran = new Random();

	public RunLitService() {
		super();
	}

	public static void main(String[] args) {
		WsUtil.loadProxyProperties();
		// addAuthors();
		// addBooks();
		// updateAuthor();
		// loadAuthor();
		loadAllAuthors();
		// removeBookFromAuthor();
		// loadAuthor();
		// addBookToAuthor();
		// addNewBookToAuthor();
		getBookCount();
		// loadBooks();
		// addBooks();
		// findBooks();
	}

	private static void findBooks() {
		WsLitService s = WsUtil.createService();
		LitBookSearchItem si = new LitBookSearchItem();
		si.setAuthor("Changed");
		si.setYear("2000");
		List<LitBook> bs = s.findBooks(si);
		System.out.println("bs=" + bs);
		for (LitBook b : bs) {
			printBook(b);
		}
	}

	private static void addAuthors() {
		WsLitService s = WsUtil.createService();
		for (int i = 0; i < 10; i++) {
			s.saveAuthor(createAuthor());
		}
	}

	private static LitAuthor createAuthor() {
		try {
			LitAuthor a = new LitAuthor();
			a.setBirthday(WsUtil.createXMLGregorianCalendar("1.1.1999",
					"dd.MM.yyyy"));
			a.setCountry(Country.values()[ran.nextInt(3)]);
			a.setLanguage(Language.values()[ran.nextInt(3)]);
			a.setName("CreatedName" + System.currentTimeMillis());
			return a;
		} catch (DatatypeConfigurationException e) {
			throw new IllegalStateException("Could not create author. ", e);
		}
	}

	private static void addBooks() {
		WsLitService s = WsUtil.createService();
		for (int i = 0; i < 10; i++) {
			LitBook b = new LitBook();
			long id = System.currentTimeMillis();
			b.setTitle("Book" + id);
			b.setSubtitle("Subtitle" + id);
			b.setTranslation(true);
			b.setYear(1999 + ran.nextInt(10));
			s.saveBook(b);
		}
	}

	private static void loadBooks() {
		WsLitService s = WsUtil.createService();
		List<LitBook> bs = s
				.loadBooks(1, 5, Arrays.asList(new OrderAttribute[] {
						OrderAttribute.YEAR_ASC, OrderAttribute.TITLE_DESC,
						OrderAttribute.TITLE_DESC }));
		System.out.println("bs=" + bs);
		for (LitBook b : bs) {
			printBook(b);
		}
	}

	private static void printBook(LitBook b) {
		if (b == null) {
			System.out.println("null");
		} else {
			System.out
					.println(""
							+ b.getTitle()
							+ " "
							+ b.getYear()
							+ " "
							+ (b.getAuthor() == null ? "null" : b.getAuthor()
									.getName()));
		}
	}

	private static void getBookCount() {
		WsLitService s = WsUtil.createService();
		long c = s.getBookCount();
		System.out.println("c=" + c);
	}

	private static void updateAuthor() {
		try {
			WsLitService s = WsUtil.createService();
			int id = 6;
			LitAuthor a = s.loadAuthor(id);
			a.setName("ChangedAt" + System.currentTimeMillis());
			a.setBirthday(WsUtil.createXMLGregorianCalendar("13.12.1965",
					"dd.MM.yyyy"));
			s.saveAuthor(a);
			LitAuthor a1 = s.loadAuthor(id);
			System.out.println("a1.name=" + a1.getName());
		} catch (DatatypeConfigurationException e) {
			throw new IllegalStateException("Could not update author. ", e);
		}
	}

	private static void addBookToAuthor() {
		WsLitService s = WsUtil.createService();
		int aid = 6;
		int bid = 2;
		LitAuthor a = s.loadAuthor(aid);
		LitBook b = s.loadBook(bid);
		s.addBookToAuthor(a, b);
		LitAuthor a1 = s.loadAuthor(aid);
		System.out.println("after a1.books.size=" + a1.getBooks().size());
	}

	private static void addNewBookToAuthor() {
		WsLitService s = WsUtil.createService();
		int aid = 6;
		LitAuthor a = s.loadAuthor(aid);
		LitBook b = new LitBook();
		b.setTitle("NewAddedBook" + System.currentTimeMillis());
		s.addBookToAuthor(a, b);
		LitAuthor a1 = s.loadAuthor(aid);
		System.out.println("after a1.books.size=" + a1.getBooks().size());
	}

	private static void removeBookFromAuthor() {
		WsLitService s = WsUtil.createService();
		int aid = 6;
		LitAuthor a = s.loadAuthor(aid);
		LitBook b = a.getBooks().get(0);
		s.removeBookFromAuthor(a, b);
		LitAuthor a1 = s.loadAuthor(aid);
		System.out.println("after a1.books.size=" + a1.getBooks().size());
	}

	private static void loadAuthor() {
		WsLitService s = WsUtil.createService();
		LitAuthor a = s.loadAuthor(6);
		System.out.println("a.name=" + a.getName());
		if (a.getBooks().isEmpty()) {
			System.out.println("NO Books");
		} else {
			for (LitBook b : a.getBooks()) {
				System.out.println("b.title=" + b.getTitle());
			}
		}
	}

	private static void loadAllAuthors() {
		WsLitService s = WsUtil.createService();
		List<LitAuthor> as = s.loadAllAuthors();
		System.out.println("as=" + as);
	}

}
